package org.osll.tictactoe.client.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.osll.tictactoe.Control;
import org.osll.tictactoe.Move;
import org.osll.tictactoe.Team;
import org.osll.tictactoe.engine.GameOverException;
import org.osll.tictactoe.engine.MoveImpl;

public class Canvas extends JPanel implements ChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4792798319512293287L;
	private Field field = null;
	private Control control = null;
	private Team team = null;
	
	public Canvas(final Field field, final Control control, final StatusBar statusBar, final Team team) {
		this.field = field;
		this.control = control;
		this.team = team;
		
		addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent e) {
        		if(control.isOver() || !control.isStarted()) {
        			return;
        		}
                int x = e.getX();
                int y = e.getY();
         
                int dimX = field.width();
                int dimY = field.height();
                
                int width = getWidth();
                int height = getHeight();
                
                int xSize = width/dimX;
                int ySize = height/dimY;
                
                int col = x / xSize;
                int row = y / ySize;
                
                Move move = new MoveImpl(row,col,team);
                
                try {
                	statusBar.setText("Отправка хода на сервер");
                	control.makeMove(row,col);
                } catch (GameOverException ex) {
                	field.addMove(move);
                	repaint();
					JOptionPane.showMessageDialog(null,
						    "Игра закончена. Вы выиграли.",
						    "Внимание",
						    JOptionPane.WARNING_MESSAGE);
					statusBar.setText("Игра окончена. Вы выиграли.");
					return;
				} catch (IllegalStateException ex) {
					JOptionPane.showMessageDialog(null,
						    ex.getLocalizedMessage(),
						    "Внимание",
						    JOptionPane.WARNING_MESSAGE);
					return;
				}
                              
				field.addMove(move);
                statusBar.setText("Ожидание хода противника");

                repaint();
            }
        });
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		int dimX = field.width();
		int dimY = field.height();
        
        int width = getWidth();
        int height = getHeight();
        
        int xSize = width/dimX;
        int ySize = height/dimY;
        
        for(int row=0;row<dimX;++row)
        	for(int col=0;col<dimY;++col) {
               	Team state = field.get(row, col);
               	Rectangle r = new Rectangle(col*xSize, row*ySize, xSize, ySize);
               	Rectangle r2 = new Rectangle(col*xSize+2, row*ySize+2, xSize-3, ySize-3);
               	

               	g.setColor(Color.darkGray);
           		((Graphics2D) g).draw(r);
           		int x1 = col*xSize;
           		int y1 = row*ySize;
               	if(state==Team.X) {
               		g.setColor(Color.blue);
               		((Graphics2D) g).drawLine(x1,y1,x1+xSize,y1+ySize);
               		((Graphics2D) g).drawLine(x1+xSize,y1,x1,y1+ySize);
               		((Graphics2D) g).drawLine(x1+1,y1,x1+xSize+1,y1+ySize);
               		((Graphics2D) g).drawLine(x1+xSize-1,y1,x1-1,y1+ySize);
               	} else if(state == Team.O) {
               		g.setColor(Color.red);
               		((Graphics2D) g).drawOval(x1, y1, xSize, ySize);
               		((Graphics2D) g).drawOval(x1, y1, xSize-1, ySize-1);
               	}
		    }	
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		repaint();
	}
	
	
}
